package com.lh.java.basic.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

class Person {

	public Person() {

	}

	public Person(String name) {
		this.name = name;
	}

	public Person(int age) {
		this.age = age;
	}

	public Person(String name, int age) {
		this.age = age;
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	@Override
	public String toString() {
		return "[" + this.name + "  " + this.age + "]";
	}

	private String name;
	private int age;
	public void setName(String name) {
		this.name = name;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

public class CreateObjectByConstructor {
	public static void main(String[] args) {
		Class<?> demo = null;
		try {
			demo = Class.forName("com.lh.reflect.Person");
		} catch (Exception e) {
			e.printStackTrace();
		}
		Person per1 = null;
		Person per2 = null;
		Person per3 = null;
		Person per4 = null;
		// 取得全部的构造函数
		Constructor<?> cons[] = demo.getConstructors();
//		for (int i = 0; i < cons.length; i++) {
//            System.out.println("构造方法：  "+cons[i]);
//        }
		 for (int i = 0; i < cons.length; i++) {
	            Class<?> p[]=cons[i].getParameterTypes();
	            System.out.print("构造方法：  ");
	            int mo=cons[i].getModifiers();
	            System.out.print(Modifier.toString(mo)+" ");
	            System.out.print(cons[i].getName());
	            System.out.print("(");
	            for(int j=0;j<p.length;++j){
	                System.out.print(p[j].getName()+" arg"+i);
	                if(j<p.length-1){
	                    System.out.print(",");
	                }
	            }
	            System.out.println("){}");
		 }
		try {
			per1 = (Person)Person.class.newInstance();
			//per1 = (Person) cons[0].newInstance();
			per1.setAge(88);
			per1.setName("hahago");
			//per2 = (Person) Person.class.getConstructor(String.class).newInstance("hahga");
			//per3 = (Person) cons[2].newInstance(20);
			//per4 = (Person) cons[3].newInstance("Rollen", 20);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(per1);
		//System.out.println(per2);
		//System.out.println(per3);
		//System.out.println(per4);
		
		Class <?> other  = null;
		try {			
			other = Class.forName("java.lang.String");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Constructor<?> cons1[] = other.getConstructors();
		System.out.println("length="+cons1.length);
	}
	
}